//
//  HotAnchorController.m
//  LOL视频播放
//
//  Created by 花花蔡 on 2016/12/8.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

#import "HotAnchorController.h"
#import "Public.h"
#import "HotAnchorTableViewCell.h"
#import "DetailTableViewController.h"
#import "URL.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "NetWorkingTool.h"
#import "HotAnchorModel.h"
@interface HotAnchorController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation HotAnchorController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray new];
    [self loadMoreData];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 加载数据
- (void)loadMoreData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[NetWorkingTool sharedTools] requestMethod:GET WithURL:[NSString stringWithFormat:@"%@",Anchor_URL] parameters:nil callBack:^(id data, NSError *error) {
            //解析
            if (error) {
                [self.tableView.mj_header endRefreshing];
            }
            
            NSDictionary *Data = data[@"Data"];
            NSArray *Result = Data[@"Result"];
            _dataArray = [HotAnchorModel mj_objectArrayWithKeyValuesArray:Result];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    
}

#pragma mark - tableView 代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HotAnchorTableViewCell *cell = [HotAnchorTableViewCell loadCellWithTableView:tableView];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    DetailTableViewController *dVC = [[DetailTableViewController alloc] init];
    dVC.model = _dataArray[indexPath.row];
    [self.navigationController pushViewController:dVC animated:YES];
}
#pragma mark - 懒加载
-(UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 110 * KWidth_ScaleW;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 50, 0, 5);
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

@end
